package com.badibadi.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import us.pinguo.edit.sdk.PGEditActivity;
import us.pinguo.edit.sdk.base.PGEditResult;
import us.pinguo.edit.sdk.base.PGEditSDK;
import us.pinguo.edit.sdk.core.utils.BitmapUtils;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.badibadi.infos.ImageAttribute;
import com.badibadi.logic.ImgFileListActivity;
import com.badibadi.mytools.IOUtils;
import com.badibadi.mytools.SelectPicLayoutActivity;
import com.badibadi.uniclubber.R;

//选择照片的
public class SelectPicts_Activity extends SelectPicLayoutActivity {
	private String timeString;// 返回的图片名称
	private String panduan;
	private String club;
	private String no_dou;
	private String activity_id;
	private ArrayList<String> filelist;
	private String photoid;
	private String mPicturePath;

	private static final int REQUEST_CODE_PICK_PICTURE = 34;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		panduan = getIntent().getStringExtra("panduan");
		club = getIntent().getStringExtra("club");
		no_dou = getIntent().getStringExtra("no_dou");
		photoid = getIntent().getStringExtra("photoid");
		activity_id = getIntent().getStringExtra("activity_id");
		takePhotoBtn = (Button) findViewById(R.id.btn_take_photo);
		pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo);
		cancelBtn = (Button) findViewById(R.id.btn_cancel);
		filelist = new ArrayList<String>();
		takePhotoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/**
				 * 下面这句还是老样子，调用快速拍照功能，至于为什么叫快速拍照，大家可以参考如下官方
				 * 文档，you_sdk_path/docs/guide/topics/media/camera.html
				 * 我刚看的时候因为太长就认真看，其实是错的，这个里面有用的太多了，所以大家不要认为
				 * 官方文档太长了就不看了，其实是错的，这个地方小马也错了，必须改正
				 */
				Date date = new Date(System.currentTimeMillis());
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"'IMG'_yyyyMMddHHmmss");
				timeString = dateFormat.format(date);
				// 创建存储图片的路径，没有sd卡就不能使用照相功能
				if (!IOUtils.hasSDcard()) {
					// "没有内存卡，无法使用相机功能"
					Toast.makeText(SelectPicts_Activity.this,
							getResources().getString(R.string.l_xb139), 3000)
							.show();
					return;
				}
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
						Environment.getExternalStorageDirectory()
								+ "/DCIM/Camera", timeString + ".jpg")));
				System.out.println( Uri.fromFile(new File(
						Environment.getExternalStorageDirectory()
								+ "/DCIM/Camera", timeString + ".jpg")));

				

//				String folderPath = Environment
//						.getExternalStoragePublicDirectory(
//								Environment.DIRECTORY_DCIM).getAbsolutePath()
//						+ File.separator;
//				System.out.println("folderPath" + folderPath);
//				String outFilePath = folderPath + System.currentTimeMillis()
//						+ ".jpg";
//				System.out.println("outFilePath" + outFilePath);
//				PGEditSDK.instance().startEdit(
//						SelectPicts_Activity.this,
//						PGEditActivity.class,
//
//						Environment.getExternalStorageDirectory()
//								+ "/DCIM/Camera" + timeString + ".jpg",
//						outFilePath);
//				System.out.println("mPicturePath" + mPicturePath);
				startActivityForResult(intent, 22);

				// finish();
			}
		});
		pickPhotoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SelectPicts_Activity.this,
						ImgFileListActivity.class);
				intent.putExtra("panduan", panduan);
				if (club != null) {
					intent.putExtra("club", club);
				}
				if (no_dou != null) {
					intent.putExtra("no_dou", no_dou);
				}
				if (photoid != null) {
					intent.putExtra("photoid", photoid);
				}
				if (activity_id != null) {
					intent.putExtra("activity_id", activity_id);
				}
				startActivityForResult(intent, 5);
//				String folderPath = Environment
//						.getExternalStoragePublicDirectory(
//								Environment.DIRECTORY_DCIM).getAbsolutePath()
//						+ File.separator;
//				System.out.println("folderPath" + folderPath);
//				String outFilePath = folderPath + System.currentTimeMillis()
//						+ ".jpg";
//				System.out.println("outFilePath" + outFilePath);
//				PGEditSDK.instance().startEdit(
//						SelectPicts_Activity.this,
//						PGEditActivity.class,
//
//						Environment.getExternalStorageDirectory()
//								+ "/DCIM/Camera" + timeString + ".jpg",
//						outFilePath);
//				System.out.println("mPicturePath" + mPicturePath);

				// finish();
				// Intent intent = new Intent(Intent.ACTION_PICK,
				// MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				// System.out
				// .println(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				// startActivityForResult(intent, REQUEST_CODE_PICK_PICTURE);
				// String folderPath =
				// Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
				// .getAbsolutePath() + File.separator;
				// System.out.println("folderPath"+folderPath);
				// String outFilePath = folderPath + System.currentTimeMillis()
				// + ".jpg";
				// System.out.println("outFilePath"+outFilePath);
				// PGEditSDK.instance().startEdit(SelectPicts_Activity.this,
				// PGEditActivity.class, folderPath, outFilePath);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		//
		//
		if (requestCode == 22 && resultCode == -1) {
			//
			// + "/DCIM/Camera/" + timeString + ".jpg");
			filelist.add(Environment.getExternalStorageDirectory().getPath()
					+ "/DCIM/Camera/" + timeString + ".jpg");
			//
			// + Environment.getExternalStorageDirectory().getPath()
			// + "/DCIM/Camera/" + timeString + ".jpg");
			Intent intent = new Intent(SelectPicts_Activity.this,
					UpPictures3Activity.class);
			Bundle bundle = new Bundle();
			bundle.putStringArrayList("files", filelist);
			bundle.putString("pan", "douhao");
			if (club != null) {
				bundle.putString("club", club);
			}
			if (no_dou != null) {
				bundle.putString("no_dou", no_dou);
			}
			if (photoid != null) {
				intent.putExtra("photoid", photoid);
			}
			if (activity_id != null) {
				intent.putExtra("activity_id", activity_id);
			}
			intent.putExtras(bundle);
			if (timeString != null) {
				startActivityForResult(intent, 5);
			}
			// this.finish();
		}
		if (requestCode == 5 && resultCode == 5) {
			ArrayList<String> filelist = data.getStringArrayListExtra("files");
			ImageAttribute attribute = (ImageAttribute) data
					.getSerializableExtra("img_info");
			Intent intent = new Intent();
			intent.putExtra("files", filelist);
			if (attribute != null) {
				intent.putExtra("img_info", attribute);
			}
			setResult(5, intent);
			finish();
		}

//		if (requestCode == REQUEST_CODE_PICK_PICTURE
//				&& resultCode == Activity.RESULT_OK && null != data) {
//
//			Uri selectedImage = data.getData();
//			String[] filePathColumns = new String[] { MediaStore.Images.Media.DATA };
//			Cursor c = this.getContentResolver().query(selectedImage,
//					filePathColumns, null, null, null);
//			c.moveToFirst();
//			int columnIndex = c.getColumnIndex(filePathColumns[0]);
//			mPicturePath = c.getString(columnIndex);
//			c.close();
//			return;
//		}
//
//		if (requestCode == PGEditSDK.PG_EDIT_SDK_REQUEST_CODE
//				&& resultCode == Activity.RESULT_OK) {
//
//			PGEditResult editResult = PGEditSDK.instance().handleEditResult(
//					data);
//			// 获取编辑后的缩略图
//			Bitmap thumbNail = editResult.getThumbNail();
//			// 获取编辑后的⼤大图路径
//			String resultPhotoPath = editResult.getReturnPhotoPath();
//			Intent intent = new Intent(this,
//					ModifiedImageInformationActivity.class);
//
//			intent.putExtra("imgId", photoid);
//
//			startActivity(intent);
//			Toast.makeText(this,
//					"Photo saved to:" + editResult.getReturnPhotoPath(),
//					Toast.LENGTH_LONG).show();
//
//		}
//		if (requestCode ==56) {
//			Toast.makeText(this, "Edit cancelled!", Toast.LENGTH_SHORT).show();
//		}
//		if (requestCode == PGEditSDK.PG_EDIT_SDK_REQUEST_CODE
//				&& resultCode == PGEditSDK.PG_EDIT_SDK_RESULT_CODE_CANCEL) {
//			Toast.makeText(this, "Edit cancelled!", Toast.LENGTH_SHORT).show();
//		}
//
//		if (requestCode == PGEditSDK.PG_EDIT_SDK_REQUEST_CODE
//				&& resultCode == PGEditSDK.PG_EDIT_SDK_RESULT_CODE_NOT_CHANGED) {
//			Toast.makeText(this, "Photo do not change!", Toast.LENGTH_SHORT)
//					.show();
//		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
